package com.sdkj.recruit.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;


/**
 * 文件转换
 * @author fred
 *
 */
public class UtilHelper {

	// base64字符串转byte[]
	public static byte[] base64String2ByteFun(String base64Str) {
		return Base64.decodeBase64(base64Str);
	}

	// byte[]转base64
	public static String byte2Base64StringFun(byte[] b) {
		return Base64.encodeBase64String(b);
	}

	/**
	 * base64string 转成文件
	 * @param base64String
	 * @param path
	 * @param name
	 */
	public static void base64StringToImage(String base64String, String path, String name,String fileType) {
		try {
			byte[] bytes1 = Base64.decodeBase64(base64String);
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
			BufferedImage bi1 = ImageIO.read(bais);
			File w2 = new File(path + name);// 可以是jpg,png,gif格式
			ImageIO.write(bi1, fileType, w2);// 不管输出什么格式图片，此处不需改动
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) {
		
		File f =new File("E:\\test\\1.pdf");
		
		try {
			FileInputStream in =new FileInputStream(f);
			byte[] b=new byte[in.available()];
			in.read(b);
			FileOutputStream out =new FileOutputStream(new File("E:\\test\\2.pdf"));
			out.write(b);
			out.close();
			in.close();
			//String base64 =byte2Base64StringFun(b)
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
}
